package com.leo.manage_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.User;
import com.leo.manage_system.entity.UserWechat;
import com.leo.manage_system.entity.vo.UserVo;
import com.leo.manage_system.mapper.UserMapper;
import com.leo.manage_system.mapper.UserWechatMapper;
import com.leo.manage_system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Map<String, Object> getUserPageQuery(Page<User> pageParam, UserVo query) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","username","nick_name","avatar_url","phone","skey","gmt_create","gmt_modify");
        queryWrapper.orderByDesc("gmt_create");
//        queryWrapper.orderByDesc("last_visit_time");
//        if( UserVo != null){
//            if (StringUtils.isNotEmpty(UserVo.getOpenId())) {
//                queryWrapper.eq("open_id", UserVo.getOpenId());
//            }
////            if (StringUtils.isNotEmpty(UserVo.getDescription())) {
////                queryWrapper.like("description", UserVo.getDescription());
////            }
//            if (StringUtils.isNotEmpty(UserVo.getNickName())) {
//                queryWrapper.like("nick_name", UserVo.getNickName());
//            }
//        }

        userMapper.selectPage(pageParam,queryWrapper);
//        System.out.println(pageParam.getRecords());
        List<User> records = pageParam.getRecords();

        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();

        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public int getUserTodayRegistrations() {
        Date date = new Date(); // this object contains the current date value
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(new Date());
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Date date = new Date(); // this object contains the current date value
        date = getDate(date,1);
        Date temp_date = getDate(date,-1);
        return DateApart(date,temp_date);
    }

    public Date getDate(Date date,int num){
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.DATE,num);
        Date date2=rightNow.getTime();
        return date2;
    }
    public int DateApart(Date date1,Date date2){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("gmt_create",date1);//小于
        queryWrapper.ge("gmt_create",date2);//大于
        return userMapper.selectCount(queryWrapper);
    }

}
